<% if !true?(Google::HashUtils.navigate(tests, %w[expectations custom
                                                  get success])) -%>
def expect_network_get_success(id, data = {})
  id_data = data.fetch(:name, '').include?('title') ? 'title' : 'name'
  body = load_network_result("success#{id}~#{id_data}.yaml").to_json

  request = double('request')
  allow(request).to receive(:send).and_return(http_success(body))

  debug_network "!! GET #{self_link(uri_data(id).merge(data))}"
  expect(Google::<%= product_ns -%>::Network::Get).to receive(:new)
    .with(self_link(uri_data(id).merge(data)),
          instance_of(Google::FakeAuthorization)) do |args|
    debug_network ">> GET #{args}"
    request
  end
end

def http_success(body)
  response = Net::HTTPOK.new(1.0, 200, 'OK')
  response.body = body
  response.instance_variable_set(:@read, true)
  response
end
<% end -%>
<% if object.async -%>

def expect_network_get_async(id, data = {})
<% if object.self_link_query.nil? -%>
<% if object.kind? -%>
  body = { kind: '<%= object.kind -%>' }.to_json
<% else # object.kind? -%>
  body = {}.to_json
<% end # object.kind? -%>
<% else # object.self_link_query.nil? -%>
<% if object.self_link_query.kind.nil? -%>
  body = { }.to_json
<% else -%>
  body = { kind: '<%= object.self_link_query.kind -%>' }.to_json
<% end # object.self_link_query.kind.nil? -%>
<% end # object.self_link_query.nil? -%>

  request = double('request')
  allow(request).to receive(:send).and_return(http_success(body))

  debug_network "!! #{self_link(uri_data(id).merge(data))}"
  expect(Google::<%= product_ns -%>::Network::Get).to receive(:new)
    .with(self_link(uri_data(id).merge(data)),
          instance_of(Google::FakeAuthorization)) do |args|
    debug_network ">> GET <async> #{args}"
    request
  end
end
<% end -%>
<% if !true?(Google::HashUtils.navigate(tests, %w[expectations custom
                                                  get failed])) -%>

def expect_network_get_failed(id, data = {})
  request = double('request')
  allow(request).to receive(:send).and_return(http_failed_object_missing)

  debug_network "!! #{self_link(uri_data(id).merge(data))}"
  expect(Google::<%= product_ns -%>::Network::Get).to receive(:new)
    .with(self_link(uri_data(id).merge(data)),
          instance_of(Google::FakeAuthorization)) do |args|
    debug_network ">> GET [failed] #{args}"
    request
  end
end

def http_failed_object_missing
  Net::HTTPNotFound.new(1.0, 404, 'Not Found')
end
<% end -%>
<% unless object.virtual -%>
<% if !true?(Google::HashUtils.navigate(tests, %w[expectations custom
                                                  create])) -%>

<%=
  lines(emit_rubocop(binding, :function, 'expect_network_create', :disabled))
-%>
def expect_network_create(id, expected_body, data = {})
  merged_uri = uri_data(id).merge(data)
<% unless object.async.nil? -%>
<% if object.async.operation.kind.nil? -%>
  body = {
<% else # object.async.operation.kind.nil? -%>
  body = { kind: '<%= object.async.operation.kind -%>',
<% end # object.async.operation.kind.nil? -%>
<% status = object.async.status.complete -%>
           status: '<%= status -%>', targetLink: self_link(merged_uri) }.to_json
<% else -%>
<% if object.kind? -%>
  body = { kind: '<%= object.kind -%>' }.to_json
<% else # object.kind? -%>
  body = {}.to_json
<% end # object.kind? -%>
<% end -%>

<% unless object.all_resourcerefs.empty? -%>
  # Remove refs that are also part of the body
  expected_body = Hash[expected_body.map do |k, v|
    [k.is_a?(Symbol) ? k.id2name : k, v]
  end]

<% end # all_resourcerefs.empty? -%>
<% if object.encoder? -%>
  # Encode the object to conform with the API
<% encoder = object.transport.encoder -%>
  expected_body = <%= encoder -%>(expected_body)
<% end # object.encoder? -%>
  request = double('request')
  allow(request).to receive(:send).and_return(http_success(body))

<% if object.create_verb.nil? || object.create_verb == :POST -%>
  debug_network "!! POST #{collection(merged_uri)}"
  expect(Google::<%= product_ns -%>::Network::Post).to receive(:new)
    .with(collection(merged_uri), instance_of(Google::FakeAuthorization),
          'application/json', expected_body.to_json) do |args|
    debug_network ">> POST #{args} = body(#{body})"
<% elsif object.create_verb == :PUT -%>
  debug_network "!! PUT #{self_link(merged_uri)}"
  expect(Google::<%= product_ns -%>::Network::Put).to receive(:new)
    .with(self_link(merged_uri), instance_of(Google::FakeAuthorization),
          'application/json', expected_body.to_json) do |args|
    debug_network ">> PUT #{args} = body(#{body})"
<% end -%>
    request
  end
end
<%=
  lines(emit_rubocop(binding, :function, 'expect_network_create', :enabled))
-%>
<% end -%>
<% if !true?(Google::HashUtils.navigate(tests, %w[expectations custom
                                                  delete])) -%>

def expect_network_delete(id, name = nil, data = {})
  delete_data = uri_data(id).merge(data)
  delete_data[:name] = name unless name.nil?
<% unless object.async.nil? -%>
<% if object.async.operation.kind.nil? -%>
  body = {
<% else # object.async.operation.kind.nil? -%>
  body = { kind: '<%= object.async.operation.kind -%>',
<% end # object.async.operation.kind.nil? -%>
           status: '<%= object.async.status.complete -%>',
           targetLink: self_link(delete_data) }.to_json
<% else -%>
<% if object.kind? -%>
  body = { kind: '<%= object.kind -%>' }.to_json
<% else # object.kind? -%>
  body = {}.to_json
<% end # object.kind? -%>
<% end -%>

  request = double('request')
  allow(request).to receive(:send).and_return(http_success(body))

  debug_network "!! DELETE #{self_link(delete_data)}"
  expect(Google::<%= product_ns -%>::Network::Delete).to receive(:new)
    .with(self_link(delete_data),
          instance_of(Google::FakeAuthorization)) do |args|
    debug_network ">> DELETE #{args}"
    request
  end
end
<% end -%>
<% end -%>

def load_network_result(file)
  results = File.join(File.dirname(__FILE__), 'data', 'network',
                      '<%= object.out_name -%>', file)
  debug("Loading result file: #{results}")
  raise "Network result data file #{results}" unless File.exist?(results)
  data = YAML.safe_load(File.read(results))
  raise "Invalid network results #{results}" unless data.class <= Hash
  data
end

<%= lines(compile('templates/resourceref_mocks.erb'), 1) -%>
def debug(message)
  puts(message) if ENV['RSPEC_DEBUG']
end

def debug_network(message)
  puts("Network #{message}") \
    if ENV['RSPEC_DEBUG'] || ENV['RSPEC_HTTP_VERBOSE']
end
